<template>
    <div class="wrapper">
        <!-- header部分 -->
        <header>
            <p>修改菜品价格</p>
        </header>

        <!-- 表单部分 -->
        <div class="form-box">
            <li>
                <div class="title">食物编号：</div>
                <div class="content">
                    <input type="number" v-model="food.foodId" placeholder="食物编号" />
                </div>
            </li>
            <li>
                <div class="title">旧食物价格：</div>
                <div class="content">
                    <input type="number" v-model="oldFoodPrice" placeholder="旧食物价格" />
                </div>
            </li>
            <li>
                <div class="title">新食物价格：</div>
                <div class="content">
                    <input type="number" v-model="newFoodPrice" placeholder="新食物价格" />
                </div>
            </li>
        </div>

        <!-- 保存按钮 -->
        <div class="save-button" @click="toUpdateFoodPrice">
            <button>保存</button>
        </div>

        <!-- 底部菜单部分 -->
        <BusinessFooter />
    </div>
</template>

<script>
import { ref } from 'vue';
import BusinessFooter from '../components/BusinessFooter.vue';
import axios from 'axios';

export default {
    name: 'ChangeMenu',
    components: {
        BusinessFooter,
    },
    setup() {
        const food = ref({
            foodId: '',
            foodName: '',
            foodExplain: '',
            foodPrice: '',
            businessId: '',
            remarks: '',
        });
        
        const oldFoodPrice = ref('');
        const newFoodPrice = ref('');

        const toUpdateFoodPrice = () => {
            if (!food.value.foodId) {
                alert('请输入食物编号！');
                return;
            }
            if (!oldFoodPrice.value) {
                alert('旧价格不能为空！');
                return;
            }
            if (!newFoodPrice.value) {
                alert('请输入新价格！');
                return;
            }

            axios.get(`/Food/foodId/${food.value.foodId}/foodPrice/${newFoodPrice.value}`)
                .then(response => {
                    if (response.data > 0) {
                        alert('修改成功');
                    } else {
                        alert('修改失败');
                    }
                })
                .catch(error => {
                    console.error(error);
                });
        };

        return {
            food,
            oldFoodPrice,
            newFoodPrice,
            toUpdateFoodPrice,
        };
    },
};
</script>

<style scoped>
/****************** 总容器 ******************/
.wrapper {
    width: 100%;
    height: 100%;
}

/****************** header ******************/
.wrapper header {
    width: 100%;
    height: 12vw;
    background-color: #0097FF;
    color: #fff;
    font-size: 4.8vw;
    position: fixed;
    left: 0;
    top: 0;
    z-index: 1000;
    display: flex;
    justify-content: center;
    align-items: center;
}

/****************** 表单部分 ******************/
.wrapper .form-box {
    width: 100%;
    padding-top: 12vw;
}
.wrapper .form-box li {
    display: flex;
    align-items: center;
    box-sizing: border-box;
    padding: 3vw 3vw 0;
}
.wrapper .form-box li .title {
    flex: 0 0 18vw;
    font-size: 3.5vw;
    font-weight: 700;
    color: darkslategray;
}
.wrapper .form-box li .content {
    flex: 1;
}
.wrapper .form-box li .content input {
    width: 100%;
    box-sizing: border-box;
    padding: 4vw 2vw;
    border-radius: 3vw;
    background-color: white;
    font-size: 3vw;
    border: none;
    outline: none;
}

/****************** 保存按钮部分 ******************/
.wrapper .save-button {
    width: 100%;
    box-sizing: border-box;
    padding: 4vw 10vw 0;
}
.wrapper .save-button button {
    width: 100%;
    height: 10vw;
    background-color: mediumseagreen;
    border: none;
    outline: none;
    border-radius: 4vw;
    font-size: 3.5vw;
    color: white;
    cursor: pointer;
}
</style>
